﻿using LpbPrj.Client.Data;
using LpbPrj.Client.Tools.Helper;
using System;
using System.Printing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace LpbPrj.Client.Views
{
    /// <summary>
    /// ReportPagePrintControl.xaml 的交互逻辑
    /// </summary>
    public partial class ReportPagePrintControl : UserControl
    {
        public ReportPagePrintControl()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        int pageHeight = 1123;
        int pageWidth = 794;

        /// <summary>
        /// 选择打印机打印,打印连续的页面
        /// 用于PC
        /// </summary>
        public void PrintPageEx()
        {
            if (printDialogEx == null)
            {
                var printDialogExNew = new PrintDialog();
                if (printDialogExNew.ShowDialog() == true)
                {
                    printDialogExNew.PrintTicket.PageOrientation = PageOrientation.Portrait;
                    printDialogExNew.PrintQueue.CurrentJobSettings.CurrentPrintTicket.PageOrientation = PageOrientation.Portrait;

                    double width = (double)printDialogExNew.PrintQueue.CurrentJobSettings.CurrentPrintTicket.PageMediaSize.Width;
                    double height = (double)printDialogExNew.PrintQueue.CurrentJobSettings.CurrentPrintTicket.PageMediaSize.Height;

                    //A4纸大小 width=794  height=1123
                    zoom = Math.Min(height / pageHeight, width / pageWidth);

                    // Resize it.
                    this.PrintGrid.LayoutTransform = new ScaleTransform(zoom, zoom);

                    // Get the size of the page.
                    System.Windows.Size pageSize = new System.Windows.Size(pageWidth * zoom, pageHeight * zoom);

                    // Trigger the sizing of the element.                                    
                    this.PrintGrid.Measure(pageSize);
                    this.PrintGrid.Arrange(new Rect(0, 0, pageSize.Width, pageSize.Height));
                    this.PrintGrid.UpdateLayout();

                    printDialogExNew?.PrintVisual(this.PrintGrid, string.Empty);
                    printDialogEx = printDialogExNew;
                }

                return;
            }

            if (printDialogEx != null)
            {
                // Resize it.
                this.PrintGrid.LayoutTransform = new ScaleTransform(zoom, zoom);
                // Get the size of the page.
                System.Windows.Size pageSize = new System.Windows.Size(pageWidth * zoom, pageHeight * zoom);
                // Trigger the sizing of the element.                                    
                this.PrintGrid.Measure(pageSize);
                this.PrintGrid.Arrange(new Rect(0, 0, pageSize.Width, pageSize.Height));
                this.PrintGrid.UpdateLayout();

                printDialogEx?.PrintVisual(this.PrintGrid, string.Empty);
            }
        }
        PrintDialog printDialogEx = null;//用于连续打印
        double zoom = 1.0;

        /// <summary>
        /// 选择打印机打印
        /// 用于PC
        /// </summary>
        public void PrintPage()
        {
            PrintDialog printDialog = new PrintDialog();
            if (printDialog.ShowDialog() == true)
            {
                printDialog.PrintTicket.PageOrientation = PageOrientation.Portrait;
                printDialog.PrintQueue.CurrentJobSettings.CurrentPrintTicket.PageOrientation = PageOrientation.Portrait;

                double width = (double)printDialog.PrintQueue.CurrentJobSettings.CurrentPrintTicket.PageMediaSize.Width;
                double height = (double)printDialog.PrintQueue.CurrentJobSettings.CurrentPrintTicket.PageMediaSize.Height;

                //A4纸大小 width=794  height=1123
                double zoom = Math.Min(height / pageHeight, width / pageWidth);

                // Resize it.
                this.PrintGrid.LayoutTransform = new ScaleTransform(zoom, zoom);

                // Get the size of the page.
                System.Windows.Size pageSize = new System.Windows.Size(pageWidth * zoom, pageHeight * zoom);

                // Trigger the sizing of the element.                                    
                this.PrintGrid.Measure(pageSize);
                this.PrintGrid.Arrange(new Rect(0, 0, pageSize.Width, pageSize.Height));
                this.PrintGrid.UpdateLayout();

                // Print the element.
                printDialog.PrintVisual(this.PrintGrid, string.Empty);

                // Reset the canvas.
                this.PrintGrid.LayoutTransform = null;
            }
        }

        /// <summary>
        /// 使用默认打印机打印
        /// 用于Touch
        /// </summary>
        public void Print(bool printResult = true)
        {
            bool ret = PrintHepler.Print(this, new System.Windows.Size(pageWidth, pageHeight));
            string info = ret ? "打印成功" : "打印失败";
            if (printResult) HandyControl.Controls.MessageBox.Show(info, GlobalData.Config.SoftTitle, MessageBoxButton.OK);
        }

        /// <summary>
        /// 结果展示的控件
        /// </summary>
        public static readonly DependencyProperty ResultControlTypeProperty = DependencyProperty.Register("ResultControl", typeof(UserControl), typeof(ReportPagePrintControl), new PropertyMetadata(null));
        public UserControl ResultControl { get => (UserControl)GetValue(ResultControlTypeProperty); set => SetValue(ResultControlTypeProperty, value); }
    }
}
